package com.aode.music.view.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;

import com.aode.music.R;
import com.aode.music.databinding.ActivityMainBinding;
import com.aode.music.view.fragment.mainfragment.ChatFragment;
import com.aode.music.view.fragment.mainfragment.HomeFragment;
import com.aode.music.view.fragment.mainfragment.MeFragment;
import com.aode.music.view.fragment.mainfragment.MusicFragment;

import java.util.ArrayList;
import java.util.List;

import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener;


public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding mainBinding;
    private List<Fragment> mFragments;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main); //初始化Fragment
        initFragment();
        commitAllowingStateLoss(0);
        //初始化底部Button
        initBottomTab();
    }

    private void initFragment() {
        mFragments = new ArrayList<>();
        mFragments.add(new HomeFragment());
        mFragments.add(new MusicFragment());
        mFragments.add(new ChatFragment());
        mFragments.add(new MeFragment());
    }



    private void initBottomTab() {
        NavigationController navigationController = mainBinding.pagerBottomTab.material()
                .addItem(R.drawable.home,"首页")
                .addItem(R.drawable.music,"音乐")
                .addItem(R.drawable.soal,"聊天")
                .addItem(R.drawable.me,"我的")
                .setDefaultColor(ContextCompat.getColor(this,R.color.textColorVice))
                .build();
        //底部按钮的点击事件监听
        navigationController.addTabItemSelectedListener(new OnTabItemSelectedListener() {
            @Override
            public void onSelected(int index, int old) {
                commitAllowingStateLoss(index);
            }

            @Override
            public void onRepeat(int index) {
            }
        });

    }

    private void commitAllowingStateLoss(int position) {
        hideAllFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(position + "");
        if (currentFragment != null) {
            transaction.show(currentFragment);
        } else {
            currentFragment = mFragments.get(position);
            transaction.add(R.id.frameLayout, currentFragment, position + "");
        }
        transaction.commitAllowingStateLoss();
    }
    //隐藏所有Fragment
    private void hideAllFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < mFragments.size(); i++) {
            Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(i + "");
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
        }
        transaction.commitAllowingStateLoss();
    }
}